package com.chaodriod.common.system;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Created by tangenlu  on 2018/3/20.
 */

public class TimeChangeReceiver extends BroadcastReceiver {
    private TimeChangeCallback mTimeChangeCallback;

    public TimeChangeReceiver(TimeChangeCallback timeChangeCallback) {
        mTimeChangeCallback = timeChangeCallback;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
            case Intent.ACTION_TIME_TICK:
                //每过一分钟 触发
                mTimeChangeCallback.onMinuteCallback();
                break;
            case Intent.ACTION_TIME_CHANGED:
                //设置了系统时间
                mTimeChangeCallback.setSysTimeCallback();
                break;
        }
    }

    public IntentFilter getIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_TIME_TICK);//每分钟变化
        intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);//设置了系统时区
        intentFilter.addAction(Intent.ACTION_TIME_CHANGED);//设置了系统时间
        return intentFilter;
    }

    public interface TimeChangeCallback {
        void onMinuteCallback();

        void setSysTimeCallback();
    }
}
